﻿namespace com.crownpack.customfld.ItemEventReceiver
{
    using System;
    using System.Collections.Generic;
    using System.Linq;

    using com.crownpack.Extensions.SharePointExtensions;

    using Microsoft.SharePoint;

    /// <summary>
    /// List Item Events
    /// </summary>
    public class ItemEventReceiver : SPItemEventReceiver
    {
        const string FIELDTYPENAME = "CPCustomer";
        #region Methods

        /// <summary>
        /// An item was added.
        /// </summary>
        public override void ItemAdded( SPItemEventProperties properties )
        {
            update( properties );
        }

        /// <summary>
        /// An item was updated.
        /// </summary>
        public override void ItemUpdated( SPItemEventProperties properties )
        {
            update( properties );
        }

        string getInternalName( SPItemEventProperties properties )
        {
            string internalName = string.Empty;
            using ( SPSite spSite = new SPSite( properties.WebUrl ) )
            {
                using ( SPWeb spWeb = spSite.OpenWeb( ) )
                {
                    SPList spList = properties.List;
                    List<SPField> fields = spList.Fields.Cast<SPField>( ).ToList( );
                    foreach ( SPField field in fields )
                    {
                        if ( field.FieldTypeDefinition.TypeName.Equals( FIELDTYPENAME, StringComparison.OrdinalIgnoreCase ) )
                        {
                            internalName = field.InternalName;
                            break;
                        }
                    }
                }
            }
            return internalName;
        }

        void update( SPItemEventProperties properties )
        {
            string internalName = getInternalName( properties );
            if ( !string.IsNullOrEmpty( internalName ) )
            {
                bool update = false;

                SPListItem spListItem = properties.ListItem;

                CustomerFieldValue v = new CustomerFieldValue( spListItem[ internalName ], properties.WebUrl );
                if ( v == null )
                    return;

                Customer o = v.UnderlyingObject;
                if ( o == null )
                    return;

                if ( spListItem.ContainsField( Layout.GetInternalName( Layout.Address1 ) ) )
                {
                    spListItem[ Layout.GetInternalName( Layout.Address1 ) ] = o.Address1;
                    update = true;
                }
                if ( spListItem.ContainsField( Layout.GetInternalName( Layout.Branch ) ) )
                {
                    spListItem[ Layout.GetInternalName( Layout.Branch ) ] = o.Branch;
                    update = true;
                }
                if ( spListItem.ContainsField( Layout.GetInternalName( Layout.BranchManager ) ) )
                {
                    spListItem[ Layout.GetInternalName( Layout.BranchManager ) ] = o.BranchManager;
                    update = true;
                }
                if ( spListItem.ContainsField( Layout.GetInternalName( Layout.BranchName ) ) )
                {
                    spListItem[ Layout.GetInternalName( Layout.BranchName ) ] = o.BranchName;
                    update = true;
                }
                if ( spListItem.ContainsField( Layout.GetInternalName( Layout.City ) ) )
                {
                    spListItem[ Layout.GetInternalName( Layout.City ) ] = o.City;
                    update = true;
                }
                if ( spListItem.ContainsField( Layout.GetInternalName( Layout.DivisionCode ) ) )
                {
                    spListItem[ Layout.GetInternalName( Layout.DivisionCode ) ] = o.DivisionCode;
                    update = true;
                }
                if ( spListItem.ContainsField( Layout.GetInternalName( Layout.GroupCode ) ) )
                {
                    spListItem[ Layout.GetInternalName( Layout.GroupCode ) ] = o.GroupCode;
                    update = true;
                }
                if ( spListItem.ContainsField( Layout.GetInternalName( Layout.LocationName ) ) )
                {
                    spListItem[ Layout.GetInternalName( Layout.LocationName ) ] = o.LocationName;
                    update = true;
                }
                if ( spListItem.ContainsField( Layout.GetInternalName( Layout.LocationNo ) ) )
                {
                    spListItem[ Layout.GetInternalName( Layout.LocationNo ) ] = o.LocationNo;
                    update = true;
                }
                if ( spListItem.ContainsField( Layout.GetInternalName( Layout.ManagerEMail ) ) )
                {
                    spListItem[ Layout.GetInternalName( Layout.ManagerEMail ) ] = o.ManagerEMail;
                    update = true;
                }
                if ( spListItem.ContainsField( Layout.GetInternalName( Layout.NA_ManagerEmailAddress ) ) )
                {
                    spListItem[ Layout.GetInternalName( Layout.NA_ManagerEmailAddress ) ] = o.NA_ManagerEmailAddress;
                    update = true;
                }
                if ( spListItem.ContainsField( Layout.GetInternalName( Layout.NA_ManagerName ) ) )
                {
                    spListItem[ Layout.GetInternalName( Layout.NA_ManagerName ) ] = o.NA_ManagerName;
                    update = true;
                }
                if ( spListItem.ContainsField( Layout.GetInternalName( Layout.NA_ManagerNo ) ) )
                {
                    spListItem[ Layout.GetInternalName( Layout.NA_ManagerNo ) ] = o.NA_ManagerNo;
                    update = true;
                }
                if ( spListItem.ContainsField( Layout.GetInternalName( Layout.NA_ManagerPhone ) ) )
                {
                    spListItem[ Layout.GetInternalName( Layout.NA_ManagerPhone ) ] = o.NA_ManagerPhone;
                    update = true;
                }
                if ( spListItem.ContainsField( Layout.GetInternalName( Layout.ParentCustomerName ) ) )
                {
                    spListItem[ Layout.GetInternalName( Layout.ParentCustomerName ) ] = o.ParentCustomerName;
                    update = true;
                }
                if ( spListItem.ContainsField( Layout.GetInternalName( Layout.ParentCustomerNo ) ) )
                {
                    spListItem[ Layout.GetInternalName( Layout.ParentCustomerNo ) ] = o.ParentCustomerNo;
                    update = true;
                }
                if ( spListItem.ContainsField( Layout.GetInternalName( Layout.Salesrep ) ) )
                {
                    spListItem[ Layout.GetInternalName( Layout.Salesrep ) ] = o.Salesrep;
                    update = true;
                }
                if ( spListItem.ContainsField( Layout.GetInternalName( Layout.SalesrepEmailAddress ) ) )
                {
                    spListItem[ Layout.GetInternalName( Layout.SalesrepEmailAddress ) ] = o.SalesrepEmailAddress;
                    update = true;
                }
                if ( spListItem.ContainsField( Layout.GetInternalName( Layout.SalesrepName ) ) )
                {
                    spListItem[ Layout.GetInternalName( Layout.SalesrepName ) ] = o.SalesrepName;
                    update = true;
                }
                if ( spListItem.ContainsField( Layout.GetInternalName( Layout.SalesrepPhone ) ) )
                {
                    spListItem[ Layout.GetInternalName( Layout.SalesrepPhone ) ] = o.SalesrepPhone;
                    update = true;
                }
                if ( spListItem.ContainsField( Layout.GetInternalName( Layout.SectorCode ) ) )
                {
                    spListItem[ Layout.GetInternalName( Layout.SectorCode ) ] = o.SectorCode;
                    update = true;
                }
                if ( spListItem.ContainsField( Layout.GetInternalName( Layout.St ) ) )
                {
                    spListItem[ Layout.GetInternalName( Layout.St ) ] = o.St;
                    update = true;
                }
                if ( spListItem.ContainsField( Layout.GetInternalName( Layout.Zip ) ) )
                {
                    spListItem[ Layout.GetInternalName( Layout.Zip ) ] = o.Zip;
                    update = true;
                }

                if ( update )
                    spListItem.UpdateOverwriteVersion( );
            }
        }

        #endregion Methods
    }
}